package edu.ncet.desktop.client;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.dnd.ListViewDragSource;
import com.extjs.gxt.ui.client.dnd.ListViewDropTarget;
import com.extjs.gxt.ui.client.dnd.DND.Feedback;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.ListView;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.LabelField;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.core.client.GWT;

import edu.ncet.desktop.shared.QuestionModel;
import edu.ncet.desktop.shared.TypeModel;

public class QuestionTypePanel extends ContentPanel
{
	List<TypeModel> availableTypes = new ArrayList<TypeModel>();
	private ListView<TypeModel> list3;
	private ListView<TypeModel> list4;
	private ListStore<TypeModel> sourceStore;
	private ComboBox<DummyNumberModel> numbersCombo;

	public QuestionTypePanel()
	{
		setHeaderVisible( false );
		GlobalItem.getExamWindow().setHeading("Question type selection");
		add(getQuestionNumberPanel());
		add(getListPanel());
	}

	private ContentPanel getListPanel()
	{
		TableLayout listPanelLayout = new TableLayout( 2 );
		listPanelLayout.setBorder( 1 );
		listPanelLayout.setCellPadding( 5 );

		ContentPanel listPanel = new ContentPanel( listPanelLayout );
		listPanel.setHeaderVisible( false );

		LabelField label1 = new LabelField( "Available Types" );
		label1.setStyleAttribute( "color", "#E81302" );
		listPanel.add( label1 );

		LabelField label2 = new LabelField( "Selected Types" );
		label2.setStyleAttribute( "color", "#E81302" );
		listPanel.add( label2 );
		listPanel.setBorders( true );
		createListUsingAvailableTypes( listPanel );
		return listPanel;
	}

	private ContentPanel getQuestionNumberPanel()
	{
		ContentPanel quesNoPanel = new ContentPanel( new TableLayout( 2 ) );

		quesNoPanel.setHeaderVisible( false );
		LabelField noOfQuestions = new LabelField( "Number of Questions : " );
		noOfQuestions.setWidth( 150 );

		numbersCombo = new ComboBox<DummyNumberModel>();
		ListStore<DummyNumberModel> numbersStore = new ListStore<DummyNumberModel>();
		numbersCombo.setDisplayField( "number" );
		for ( int i = 1; i <= 60; i++ )
		{
			numbersStore.add( new DummyNumberModel( i ) );
		}
		numbersCombo.setWidth( 50 );
		numbersCombo.setStore( numbersStore );
		numbersCombo.setValue(new DummyNumberModel(1));

		quesNoPanel.add( noOfQuestions );
		quesNoPanel.add( numbersCombo );
		quesNoPanel.setBorders( true );
		return quesNoPanel;
	}

	private void createListUsingAvailableTypes( ContentPanel listPanel )
	{
		ListView<TypeModel> sourceTypesListView = getAvailableQuesTypeListView( listPanel );
		ListView<TypeModel> selectedTypesListView = getSelectedQuesTypeListView( listPanel );

		getAvaillableTypes();

		listPanel.add( sourceTypesListView );
		listPanel.add( selectedTypesListView );

		HorizontalPanel dummyPanel = new HorizontalPanel();
		dummyPanel.setHeight( 25 );

		listPanel.add( dummyPanel );
		listPanel.add( dummyPanel );
		listPanel.add( dummyPanel );

		listPanel.add( getStartTestButton() );
	}

	private Button getStartTestButton()
	{
		Button StartTestButton = new Button( "Start Test", new SelectionListener<ButtonEvent>()
		{
			public void componentSelected( ButtonEvent ce )
			{
				startExams();
			}
		} );
		StartTestButton.addStyleName( "x-student-question-type-selector-button" );
		return StartTestButton;
	}

	private void startExams()
	{
		List<TypeModel> selectedQuestionTypes = list4.getStore().getModels();
		
		if(selectedQuestionTypes.size() == 0)
		{
			MessageBox.alert("Question type selection", "Please select a question type !!", null);
			return;
		}
		
		int selectedQuestionNumbers = ( ( Integer ) numbersCombo.getSelection().get( 0 ).get( "number" ) );

		ExamsServiceAsync async = (ExamsServiceAsync)GWT.create( ExamsService.class );
		async.loadQuestionPaper( selectedQuestionTypes, selectedQuestionNumbers, new Callback<List<QuestionModel>>()
		{
			public void onSuccess( List<QuestionModel> result )
			{
				startExams( result );
			}
		} );
	}

	protected void startExams( List<QuestionModel> result )
	{
		ExaminationPanel exam = new ExaminationPanel();
		exam.setQuestionSheet( result );
		exam.createAnswerSheet();
		final Integer questionsEstimatedTiming = getQuestionsEstimatedTiming( result );
		exam.setQuestionPaperTiming( questionsEstimatedTiming < 120 ? 120 : questionsEstimatedTiming );
		GlobalItem.getExamWindow().setPosition(127, 50);
		GlobalItem.getExamWindow().remove( this );
		GlobalItem.getExamWindow().setHeading("Examination");
		GlobalItem.getExamWindow().add( exam );
		GlobalItem.getExamWindow().setSize(1015, 501);
		GlobalItem.getExamWindow().layout();
	}

	private Integer getQuestionsEstimatedTiming( List<QuestionModel> result )
	{
		Integer totTiming = 0;
		for ( QuestionModel qm : result )
		{
			final Integer timing = qm.getEstimatedTime();
			if ( timing != null )
				totTiming += timing;
		}
		return totTiming;
	}

	private void getAvaillableTypes()
	{
		ExamsServiceAsync async = (ExamsServiceAsync)GWT.create( ExamsService.class );
		async.getQuestionTypes( new String(), new Callback<List<TypeModel>>()
		{
			public void onSuccess( List<TypeModel> result )
			{
				sourceStore.add( result );
			}
		} );
	}

	private ListView<TypeModel> getAvailableQuesTypeListView( ContentPanel newMessagePanel )
	{
		list3 = new ListView<TypeModel>();
		list3.setDisplayProperty( "typeName" );
		list3.setHeight( 100 );
		list3.setWidth( 150 );
		sourceStore = new ListStore<TypeModel>();
		list3.setStore( sourceStore );
		newMessagePanel.add( list3 );
		ListViewDragSource source3 = new ListViewDragSource( list3 );
		source3.setGroup( "toType" );
		ListViewDropTarget target3 = new ListViewDropTarget( list3 );
		target3.setGroup( "toType" );
		target3.setFeedback( Feedback.INSERT );
		return list3;
	}

	private ListView<TypeModel> getSelectedQuesTypeListView( ContentPanel selectedTypesPanel )
	{
		ListStore<TypeModel> store;
		list4 = new ListView<TypeModel>();
		list4.setHeight( 100 );
		list4.setWidth( 150 );
		list4.setDisplayProperty( "typeName" );
		store = new ListStore<TypeModel>();
		list4.setStore( store );
		selectedTypesPanel.add( list4 );
		ListViewDragSource source4 = new ListViewDragSource( list4 );
		source4.setGroup( "toType" );
		ListViewDropTarget target4 = new ListViewDropTarget( list4 );
		target4.setGroup( "toType" );
		target4.setFeedback( Feedback.INSERT );
		return list4;
	}

}

class DummyNumberModel extends BaseModel
{
	private static final long serialVersionUID = 1L;

	public DummyNumberModel( Integer number )
	{
		set( "number", number );
	}
}